package com.kevin.controller.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kevin.common.api.PageDTO;
import com.kevin.common.api.R;
import com.kevin.service.entity.ScheduleJobLog;
import com.kevin.service.query.ScheduleJobLogQuery;
import com.kevin.service.service.ScheduleJobLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务日志
 *
 * @author 阿沐 babamu@126.com
 */
@RestController
@RequestMapping("schedule/log")
@Api(tags = "定时任务日志")
@AllArgsConstructor
public class ScheduleJobLogController {
    private final ScheduleJobLogService scheduleJobLogService;

    @GetMapping("page")
    @ApiOperation(value = "分页")
    @SaCheckPermission(value = "schedule:log")
    public R<PageInfo<ScheduleJobLog>> page(PageDTO pageDTO, ScheduleJobLogQuery query) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return R.ok(new PageInfo<>(scheduleJobLogService.list(Wrappers.lambdaQuery(ScheduleJobLog.class)
                .like(StrUtil.isNotBlank(query.getJobName()), ScheduleJobLog::getJobName, query.getJobName())
                .like(StrUtil.isNotBlank(query.getJobGroup()), ScheduleJobLog::getJobGroup, query.getJobGroup())
                .eq(query.getJobId() != null, ScheduleJobLog::getJobId, query.getJobId())
                .orderByDesc(ScheduleJobLog::getId)
        )));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "信息")
    @SaCheckPermission(value = "schedule:log")
    public R<ScheduleJobLog> get(@PathVariable("id") Long id) {
        return R.ok(scheduleJobLogService.getById(id));
    }

}
